package com.example.administrator.hellocomp;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;

public class listActivity extends Activity {

    private MediaPlayer mp;
    private String song_path;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        if (ActivityCompat.checkSelfPermission( listActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions( listActivity.this,
                    new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, 123);
            return;
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        mp = new MediaPlayer();
        ArrayList<String> list = new ArrayList<String>(); //音乐列表
        File sdpath= Environment.getExternalStorageDirectory(); //获得手机SD卡路径
        File path=new File(sdpath+"//Music//recorder//"); //获得SD卡的mp3文件夹
//返回以.mp3结尾的文件 (自定义文件过滤)
        File[ ] songFiles = path.listFiles( new MyFilter(".amr") );
        for (File file :songFiles){
            list.add( file.getAbsolutePath() ); //获取文件的绝对路径
        }


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                listActivity.this,
                android.R.layout.simple_list_item_single_choice,
                list );
        ListView li=(ListView)findViewById(R.id.li);
        li.setAdapter(adapter);
        li.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        li.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                song_path=((TextView)view).getText().toString();
                try{
                    mp.reset(); //重置
                    mp.setDataSource(song_path);
                    mp.prepare(); //准备
                    mp.start();  //播放
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        Button btn_re = (Button)findViewById(R.id.btn_re);
        btn_re.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mp.stop();
                        mp.release();
                        Toast.makeText(getApplicationContext(),"返回了哦",Toast.LENGTH_SHORT).show();
                        setResult(RESULT_OK, null);
                        finish(); //不能少
                    }
                }
        );

    }
}
